package it.unibz.ti2.internetPizzeria.persistence;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * 
 * Class from
 * http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#setup-configuration
 * Responsible for opening hibernate sessions.
 */
public class HibernateUtil {
	private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log exception!
            throw new ExceptionInInitializerError(ex);
        }
    }

    /**
     * Opens new hibernate session
     * @return new hibernate session
     * @throws HibernateException
     */
    public static Session getSession()
            throws HibernateException {
        return sessionFactory.openSession();
    }

	
}
